<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Jialeo\LaravelSchemaExtend\Schema;

class CreateOrderUserInfoTables extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {

        $cFunc = function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('orderId')->default(0)->comment("订单 Id");
            $table->unsignedBigInteger('brandId')->default(0)->comment("品牌 Id");
            $table->unsignedBigInteger('shopId')->default(0)->comment("店铺 ID");
            $table->string('provinceName', 64)->default('')->comment('收货地址-省');
            $table->string('cityName', 64)->default('')->comment('收货地址-市');
            $table->string('countyName', 64)->default('')->comment('收货地址-区');
            $table->unsignedInteger('provinceCode')->default(0)->comment('收货地址-省Code');
            $table->unsignedInteger('cityCode')->default(0)->comment('收货地址-市Code');
            $table->unsignedInteger('countyCode')->default(0)->comment('收货地址-区Code');
            $table->string('detailInfo', 200)->default('')->comment('收货地址-详细地址');
            $table->string('telNumber', 30)->default('')->comment('收货地址-联系电话');
            $table->string('userName', 30)->default('')->comment('收货地址-联系人姓名');

            $table->unsignedInteger('createdAt')->default(0)->comment('创建时间');
            $table->unsignedInteger('updatedAt')->default(0)->comment('更新时间');
            $table->unsignedInteger('createdByUid')->default(0)->comment('创建人');
            $table->unsignedInteger('updatedByUid')->default(0)->comment('更新人');
            $table->tinyInteger("isDeleted")->default(0)->comment("是否删除,0:未删除,1:已删除");

            $table->index('orderId', 'idx_orderId');
            $table->index('shopId', 'idx_shopId');

            $table->comment = '订单用户信息表' . $this->name;
        };

        // 分表库
        for ($i = 0; $i <= 99; $i++) {
            $this->name = '_' . str_pad($i, 3, 0, STR_PAD_LEFT);
            Schema::connection("mysqlOrder")->create('order_user_info' . $this->name, $cFunc);
            Schema::connection("mysqlOrder_2")->create('order_user_info' . $this->name, $cFunc);
            dump($this->name . '已添加');
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        for ($i = 0; $i <= 99; $i++) {
            $name = '_' . str_pad($i, 3, 0, STR_PAD_LEFT);
            Schema::connection("mysqlOrder")->dropIfExists('order_user_info' . $name);
            Schema::connection("mysqlOrder_2")->dropIfExists('order_user_info' . $name);

            dump($name);
        }
    }
}
